package com.example.administrator.wallet.view.activity.finds.kkbank;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.KKBankCoinAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.ExchangeBalanceBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.finds.kkbank.Detail.DetailsActivity;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class KKBankActivity extends BaseActivity {
    @Bind(R.id.finish_img)
    ImageButton bt_back;
    @Bind(R.id.view_title)
    TextView mV_title;
    @Bind(R.id.bt_Details)
    Button bt_Details;//明细
    @Bind(R.id.textView12)
    TextView tv_kk_assets;//总金额折和
    @Bind(R.id.lv_coin_balance)
    ListView lv_coin_balance;//币种余额列表
    @Bind(R.id.smart)
    SmartRefreshLayout smart;//刷新


    IRequestManager reqequestManager = RequestFactory.getReqequestManager();
    private List<ExchangeBalanceBean.DataBean.ListBean> dataBeanList;
    public KKBankCoinAdapter adapter;//KKBankCoinAdapter
    private ExchangeBalanceBean exchangeBalanceBean;

    @Override
    public void init() {
        bt_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.KKbank);
        setStatuColor();
        initData();

        smart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initData();
            }
        });

        //明细
        bt_Details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),DetailsActivity.class));
                finish();
            }
        });

        bt_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }


    public void initAdapter() {
        adapter = new KKBankCoinAdapter(KKBankActivity.this, dataBeanList);
        lv_coin_balance.setAdapter(adapter);
    }
    private void initData() {
        //获取币种余额列表
        dataBeanList = new ArrayList<>();
        reqequestManager.httpPost(Api.BaseUrl + Api.Exchange_Balance, Api.getExchangeBalance(SharedPrefsUtil.getValue(KKBankActivity.this, "wallet", "token", null)), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                exchangeBalanceBean = gson.fromJson(Response, ExchangeBalanceBean.class);
                if (exchangeBalanceBean.getCode() == 200) {
                    if (exchangeBalanceBean.getData().getList() != null) {
                        dataBeanList.addAll(exchangeBalanceBean.getData().getList());
                        tv_kk_assets.setText(exchangeBalanceBean.getData().getTotal_money());
                        initAdapter();
                        OnClick();
                        smart.finishRefresh();
                    } else {
                        initData();
                    }
                }
            }

            @Override
            public void onFailure(String error) {
                smart.finishRefresh();
            }
        });

    }
    private void OnClick() {
        adapter.getClickPst(new KKBankCoinAdapter.OnClick() {
            @Override
            public void Click(ExchangeBalanceBean.DataBean.ListBean dataBean) {
                Intent intent = new Intent(KKBankActivity.this, KKBankCurrencyBalanceActivity.class);
                intent.putExtra("Currency",dataBean.getName() );
                intent.putExtra("icon",dataBean.getIcon() );
                intent.putExtra("addr", dataBean.getAddr());
                String a = dataBean.getAvailable();
                intent.putExtra("available", String.valueOf(a));
                KKBankActivity.this.startActivity(intent);
                finish();
            }

        });

        adapter.getBt_kkbank_transfer_outClickPst(new KKBankCoinAdapter.Bt_kkbank_transfer_out_OnClick(){
            @Override
            public void Bt_kkbank_transfer_out_OnClick(ExchangeBalanceBean.DataBean.ListBean dataBean) {
                Intent intent = new Intent(getApplicationContext(), CashWithdrawalOrRechargeActivity.class);
                intent.putExtra("title",String.valueOf(R.string.Cash_withdrawal));
                intent.putExtra("Currency", dataBean.getName());
                String a = dataBean.getAvailable();
                intent.putExtra("available", String.valueOf(a));
                intent.putExtra("addr", dataBean.getAddr());
                startActivity(intent);
            }

        });
        adapter.getBt_kkbank_recharge_OnClickPst(new KKBankCoinAdapter.Bt_kkbank_recharge_OnClick(){

            @Override
            public void Bt_kkbank_recharge_OnClick(ExchangeBalanceBean.DataBean.ListBean dataBean) {
                Intent intent = new Intent(getApplicationContext(), CashWithdrawalOrRechargeActivity.class);
                intent.putExtra("title",String.valueOf(R.string.recharge));
                intent.putExtra("Currency", dataBean.getName());
                String a = dataBean.getAvailable();
                intent.putExtra("available", String.valueOf(a));
                intent.putExtra("addr", dataBean.getAddr());
                startActivity(intent);
            }

        });



        smart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (refreshlayout.isRefreshing()) {
                    initData();
                }
            }
        });

        smart.setOnLoadmoreListener(null);
        smart.setLoadmoreFinished(true);
    }
    @Override
    public void onResume() {
        super.onResume();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);

    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_kkbank;
    }
}
